<template>
  <div class="detail">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录账户设置" lazy>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="账户" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="sumbit">确定</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <!-- lazy懒加载 -->
        <el-tab-pane label="个人详情" lazy>
          <!-- <Info :user-info="initUserInfo" @setUserName="setUserName" /> -->
          <!-- 异步加载相应组件 -->
          <component
            :is="'Info'"
            :user-info="initUserInfo"
            @setUserName="setUserName"
          />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" lazy>
          <component :is="'Job'" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { sysUser } from '@/api/user.js'
import { sysUserPut } from '@/api/employees.js'
// import Info from './components/info.vue'
export default {
  components: {
    // 异步组件
    // 异步组件与动态组件结合
    Info: () => import('./components/info.vue'),
    Job: () => import('./components/job.vue')
  },
  data() {
    return {
      id: this.$route.params.id,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {
            required: true,
            mseeage: '必填',
            trigger: 'change'
          }
        ],
        password: [
          {
            required: true,
            mseeage: '必填',
            trigger: 'change'
          },
          {
            min: 6,
            max: 12,
            message: '请输入6-12位字符',
            trigger: 'change'
          }
        ]
      },
      userInfo: {},
      initUserInfo: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.id)
      this.userInfo = res.data
      this.initUserInfo = JSON.parse(JSON.stringify(res.data))
      this.form.username = res.data.username
      //   console.log('用户信息', res)
    },
    sumbit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPut({ ...this.userInfo, ...this.form })
          // 用户名修改后要同步到初始化数据，二边保持同步
          this.initUserInfo.username = this.form.username
          this.$message.success('修改成功')
        }
      })
    },
    setUserName(str) {
      this.form.username = str
    }
  }
}
</script>

<style lang="scss" scoped>
.detail {
  padding: 20px;
}
</style>
